﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ItemsGet();
    }

    public int CurrentPage
    {
        get
        {
            // look for current page in ViewState
            object o = this.ViewState["_CurrentPage"];
            if (o == null)
                return 0;	// default to showing the first page
            else
                return (int)o;
        }

        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }

    private void ItemsGet()
    {
        // Read sample item info from XML document into a DataSet

        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        using (SqlConnection Conn = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM Arabic_News ORDER BY News_Date DESC", Conn) { Connection = Conn };
            using (DataSet ds = new DataSet())
            {
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds);
                // Populate the repeater control with the Items DataSet
                PagedDataSource objPds = new PagedDataSource { DataSource = ds.Tables[0].DefaultView, AllowPaging = true, PageSize = 5, CurrentPageIndex = CurrentPage };
                lblCurrentPage.Text = String.Format("صفحة رقم: {0} من {1}", CurrentPage + 1, objPds.PageCount);
                // Disable Prev or Next buttons if necessary
                LnkPrev.Enabled = !objPds.IsFirstPage;
                LnkNext.Enabled = !objPds.IsLastPage;
                this.rptNews.DataSource = objPds;
            }
        }
        this.rptNews.DataBind();
    }

    protected void LnkPrev_Click(Object sender, EventArgs e)
    {
        // Set viewstate variable to the previous page
        CurrentPage -= 1;

        // Reload control
        ItemsGet();
    }
    protected void LnkNext_Click(Object sender, EventArgs e)
    {
        // Set viewstate variable to the next page
        CurrentPage += 1;

        // Reload control
        ItemsGet();
    }
}
